package com.lyd.sweet.mybatis.mapper;

import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * 类的用途
 *
 * @author 木木
 **/
@RegisterMapper
public class DeleteProvider extends MapperTemplate {

    private Logger logger = LoggerFactory.getLogger(DeleteProvider.class);

    public DeleteProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param ms
     * @return
     */
    public String deleteByOneField(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
        sql.append("<where>");
        sql.append("<if test=\"field != null\" >");
        sql.append(" and ${field}=#{value}");
        sql.append("</if>");
        sql.append("</where>");
        return sql.toString();
    }

    /**
     * 根据主键字段进行伪删除，方法参数必须包含完整的主键属性
     *
     * @param ms
     * @return
     */
    public String deleteByTwoField(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
        sql.append(" <where>");
        sql.append("<if test=\"oneField != null\" >");
        sql.append(" and ${oneField}=#{oneValue}");
        sql.append("</if>");
        sql.append("<if test=\"twoField != null\" >");
        sql.append(" and ${twoField}=#{twoValue}");
        sql.append("</if>");
        sql.append("</where>");

        return sql.toString();
    }

}
